package util.content;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ContentCapture {

	// 抓取字符串中的有效内容部分
	public String getContent(String contentBodyString) {
		final String reAskTagAString = "<!--reAskAnswerBeginning --><div><h5 > continue ask </h5>";
		final String reAskTagQString = "<!--reAskAnswerBeginning --><div><h5 > answer </h5>";
		final String preString = "<div><!--woshizhushistart --><div>";
		final String trailString = "</div><!--woshizhushiend --></div>";
		final String mainAskBeginning = "<div>";
		final String reAskEndingString = "</div><!--reAskAnswerEnd -->";

		// System.out.println("contentBodyString");
		String finalContent = "";
		String contentOneContentString = null;
		RandomContent rc = new RandomContent();
		// FilterSpace fs = new FilterSpace();
		// 把问题、最佳回答、普通回答分开了
		Pattern contentAreaPattern = Pattern.compile(contentArea);
		Matcher contentAreaMatcher = contentAreaPattern.matcher(contentBodyString);
		contentAreaMatcher.matches();
		contentAreaMatcher.reset();
		while (contentAreaMatcher.find()) {
			// contentAreaString——问或答、最佳的一个区块的字符串
			String contentAreaString = contentAreaMatcher.group();
			// 最佳回答、普通回答可能包含了7多个回答，多个回答又需要分开
			// System.out.println("contentAreaString");
			Pattern oneContentAreaPattern = Pattern.compile(oneContentArea);
			Matcher oneContentAreaMatcher = oneContentAreaPattern.matcher(contentAreaString);
			oneContentAreaMatcher.matches();
			oneContentAreaMatcher.reset();
			while (oneContentAreaMatcher.find()) {
				String oneQA = "";
				// System.out.println("--------------------contentAreaMatcher");
				// 拿到了一个回答包括下面的追问追答的字符串
				contentOneContentString = oneContentAreaMatcher.group();
				// 抓取出主回答不包括追问追答的部分
				Pattern mainAnswerAreaPattern = Pattern.compile(mainArea);
				Matcher mainAnswerAreaMatcher = mainAnswerAreaPattern.matcher(contentOneContentString);
				mainAnswerAreaMatcher.matches();
				mainAnswerAreaMatcher.reset();
				String mainAnswerAreaString = "";
				while (mainAnswerAreaMatcher.find()) {
					// System.out.println("--------------------mainAnswerAreaMatcher");
					mainAnswerAreaString = mainAnswerAreaMatcher.group();
				}

				Pattern toLinePatternAnswer = Pattern.compile(mainRex);
				Matcher toLineMatcherAnswer = toLinePatternAnswer.matcher(mainAnswerAreaString.replaceAll(contentdel, ""));
				toLineMatcherAnswer.matches();
				toLineMatcherAnswer.reset();
				while (toLineMatcherAnswer.find()) {
					// System.out.println("--------------------toLineMatcherAnswer");

					String oneLineString = toLineMatcherAnswer.group().replaceAll(contentdelAfterSpider, "").replaceAll("<pre.*>\\s*", "<pre>").trim() + "\n";
					if (contentAttri.length() > 3) {
						oneLineString = mainAskBeginning + oneLineString + "</div>";
					}
					// System.out.println(oneLineString);
					oneQA = oneQA + oneLineString;

				}

				// 抓取追问追答部分
				Pattern reAskPattern = Pattern.compile(reAskArea);
				Matcher reAskmatcher = reAskPattern.matcher(contentOneContentString);
				reAskmatcher.reset();
				while (reAskmatcher.find()) {
					// System.out.println("--------------------reAskmatcher");
					String reAskContentString = "";
					// 截取出一个追问追答字符串部分
					String reAskBodyString = reAskmatcher.group();
					// System.out.println(reAskBodyString);
					// 把追问追答字符串打散成一行一行
					Pattern toLinePatternReask = Pattern.compile(reAskRex);
					Matcher toLineMatcherReask = toLinePatternReask.matcher(reAskBodyString.replaceAll(contentdel, ""));
					toLineMatcherReask.matches();
					toLineMatcherReask.reset();
					while (toLineMatcherReask.find()) {
						// System.out.println("--------------------toLineMatcherReask");
						reAskContentString = toLineMatcherReask.group(2);
						reAskContentString = reAskContentString.replaceAll(contentdelAfterSpider, "").replaceAll("<pre.*>\\s*", "<pre>").trim() + "\n";
						// System.out.println(reAskContentString);
						Pattern reAskTagAPattern = Pattern.compile(reAskRexTagA);
						Pattern reAskTagQPattern = Pattern.compile(reAskRexTagQ);
						Matcher reAskTagAMatcher = reAskTagAPattern.matcher(reAskBodyString);
						Matcher reAskTagQMatcher = reAskTagQPattern.matcher(reAskBodyString);
						reAskTagAMatcher.matches();
						reAskTagAMatcher.reset();
						reAskTagQMatcher.matches();
						reAskTagQMatcher.reset();
						if (reAskTagAMatcher.find()) {
							// System.out.println("wen");
							reAskContentString = reAskTagAString + reAskContentString + reAskEndingString;
						}
						if (reAskTagQMatcher.find()) {
							// System.out.println("da");
							reAskContentString = reAskTagQString + reAskContentString + reAskEndingString;
						}
						oneQA = oneQA + reAskContentString;
					}
					// "\n   "是为了格式，每个新的追问追答新起一行并开头空4个空格

				}
				// 调用乱序对象，把内容写入
				// System.out.println(oneQA + "\n" + "<hr/>\n");
				// 如果是回答，则加上一个题目<h4>可以参考的解决方法</h4>
				if (contentAttri.length() > 3) {
					// System.out.println("<h4>参考答案</h4>");
					oneQA = preString + oneQA + trailString;
				}

				rc.contentAdd(oneQA + "\n" + "\n");
			}

		}

		// 每段内容加一个换行和分割线
		finalContent = rc.getWycContent();

		// System.out.println(finalContent);
		rc.clear();
		return finalContent;
	}

	/*
	 * // 去掉字符串中的指定部分 public String del(String content) { String
	 * contentWithoutDel = null; contentWithoutDel = content.replaceAll(delRex,
	 * ""); return contentWithoutDel; }
	 */

	String reAskArea = null;

	public void setReAskArea(String reAskArea) {
		this.reAskArea = reAskArea;
		// System.out.println(reAskArea);
	}

	String contentdel = null;

	public void setContentdel(String contentdel) {
		this.contentdel = contentdel;
		// System.out.println(contentdel);
	}

	String contentArea = null;

	public void setContentArea(String contentArea) {
		this.contentArea = contentArea;
		// System.out.println(contentArea);
	}

	String oneContentArea = null;

	public void setOneContentArea(String oneContentArea) {
		this.oneContentArea = oneContentArea;
		// System.out.println(oneContentArea);
	}

	String mainArea = null;

	public void setMainArea(String mainArea) {
		this.mainArea = mainArea;
		// System.out.println(mainArea);
	}

	String mainRex = null;

	public void setMainRex(String mainRex) {
		this.mainRex = mainRex;
	}

	String reAskRex = null;

	public void setReAskRex(String reAskRex) {
		this.reAskRex = reAskRex;
	}

	String contentdelAfterSpider = null;

	public void setContentdelAfterSpider(String contentdelAfterSpider) {
		this.contentdelAfterSpider = contentdelAfterSpider;
	}

	String contentAttri = null;

	public void setContentAttri(String contentAttri) {
		this.contentAttri = contentAttri;
		// System.out.println(contentAttri);
	}

	String reAskRexTagA = null;

	public void setReAskRexTagA(String reAskRexTagA) {
		this.reAskRexTagA = reAskRexTagA;
	}

	String reAskRexTagQ = null;

	public void setReAskRexTagQ(String reAskRexTagQ) {
		this.reAskRexTagQ = reAskRexTagQ;
	}

}
